home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / program_perfection / arexx / arexx_demo.c < prev    next >
C/C++ Source or Header  |  2000-03-14  |  3KB  |  171 lines

  1. /*
  2.  * Quick Reaction ARexx demo
  3.  *
  4.  * Richard Drummond 11/3/2000
  5.  *
  6.  * This code doesn't work :(
  7.  * For some reason, it won't create an ARexx object
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <reaction/reaction.h>
  13. #include <classes/arexx.h>
  14.  
  15. #include <proto/arexx.h>
  16.  
  17.  
  18. /*
  19.  * Our hostname
  20.  */
  21. #define HOSTNAME    "MY_HOSTNAME"
  22.  
  23.  
  24. /*
  25.  * Prototype for ARexx handler
  26.  */
  27. __saveds VOID HandleARexx( __reg("a0") struct ARexxCmd *, __reg("a1") struct RexxMsg * );
  28.  
  29.  
  30. /*
  31.  * IDs for out commands
  32.  */
  33. enum
  34. {
  35.     REXXID_LOAD,
  36.     REXXID_QUIT,
  37.     REXXID_MAX
  38. };
  39.  
  40. /*
  41.  * Internal list of commands
  42.  */
  43. struct MyCmd
  44. {
  45.     STRPTR  Command;
  46.     STRPTR  Template;
  47.     UWORD   ID;
  48. };
  49.  
  50. static CONST struct MyCmd MyCmdTable[ REXXID_MAX ] =
  51. {
  52.     "LOAD", "FILE",  REXXID_LOAD,
  53.     "QUIT", "",      REXXID_QUIT
  54. };
  55.  
  56.  
  57. /*
  58.  * BuildARexxTable
  59.  *
  60.  * Allocates an ArexxCmd table for passing to the arexx.class from
  61.  * the internatl list of commands
  62.  */
  63. struct ARexxCmd *
  64. BuildARexxTable( CONST struct MyCmd *my_table, ULONG cmd_count )
  65. {
  66.     struct ARexxCmd *arexx_table;
  67.     struct ARexxCmd *table_entry;
  68.  
  69.     if( arexx_table = table_entry = (struct ARexxCmd *) AllocVec( (cmd_count+1) * sizeof(struct ARexxCmd), MEMF_PUBLIC | MEMF_CLEAR ) )
  70.     {
  71.         do
  72.         {
  73.             cmd_count--;
  74.  
  75.             table_entry->ac_Name        = my_table->Command;
  76.             table_entry->ac_ID          = my_table->ID;
  77.             table_entry->ac_Func        = HandleARexx;
  78.             table_entry->ac_ArgTemplate = my_table->Template;
  79.  
  80.             table_entry++;
  81.             my_table++;
  82.         } while( cmd_count );
  83.     }
  84.  
  85.     return arexx_table;
  86. }
  87.  
  88.  
  89. /*
  90.  * Free said table
  91.  */
  92. VOID
  93. FreeARexxTable( struct ARexxCmd *table )
  94. {
  95.     FreeVec( table );
  96.  
  97.     return;
  98. }
  99.  
  100.  
  101. /*
  102.  * Main
  103.  */
  104. int
  105. main( void )
  106. {
  107.     struct ARexxCmd *cmds;
  108.     Object          *arexx;
  109.  
  110.  
  111.     if( cmds = BuildARexxTable( &MyCmdTable[0], REXXID_MAX ) )
  112.     {
  113.         ULONG   rexx_err = 0L;
  114.  
  115.         /*
  116.          * This should create our ARexx port . . .
  117.          */
  118.         if( arexx = (Object *) NewObject( NULL, AREXX_GetClass(),
  119.                                                 AREXX_HostName,     HOSTNAME,
  120.                                                 AREXX_NoSlot,       FALSE,
  121.                                                 AREXX_Commands,     cmds,
  122.                                                 AREXX_ErrorCode,    &rexx_err,
  123.                                             TAG_DONE ) )
  124.         {
  125.             Printf( "Successful\n" );
  126.  
  127.             /*
  128.              * Carry on with the rest  . . .
  129.              */
  130.  
  131.             DisposeObject( arexx );
  132.         }
  133.         else
  134.         {
  135.             /*
  136.              * But it doesn't work . .
  137.              *
  138.              * It doesn't even return an error.
  139.              *
  140.              * Hmmmm . . . .
  141.              */
  142.  
  143.             Printf( "DOS Error:%ld\n",   IoErr() );
  144.             Printf( "ARExx Error:%ld\n", rexx_err );
  145.         }
  146.         FreeARexxTable( cmds );
  147.     }
  148.  
  149.     return 0L;
  150. }
  151.  
  152.  
  153. /*
  154.  * This a clall-back hook which is called by arexx.class when it receives
  155.  * an ARexx message
  156.  *
  157.  * cmd = the entry in the command table correspondinhg to this command
  158.  * rm  = the message itself
  159.  */
  160.  
  161. __saveds VOID
  162. HandleARexx( __reg("a0") struct ARexxCmd *cmd, __reg("a1") struct RexxMsg *rm )
  163. {
  164.     /*
  165.      * Handle ARexx command here . . .
  166.      */
  167.  
  168.     return;
  169. }
  170.  
  171.